<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/10 0010
 * Time: 下午 10:13
 */

namespace App;

use App\System\ScanList;

class Command
{

    private $args;


    /**
     * Command constructor.
     * @param $argv
     */

    public function __construct($argv)
    {
        $this->initOs();
        $this->args = array_merge(getopt('', ['help', 'list', 'run:']), $argv);
    }

    public function run()
    {
        if (isset($this->args['help'])) {
            $this->log();
            return;
        }

        if (isset($this->args['list'])) {
            $list = ScanList::getInstance()->getCommandList();
            foreach ($list as $key => $item) {
                printf("序列：%s\t描述：%s\n", $key,$item[2]);
            }
            return;
        }
        if (isset($this->args['run'])) {
            $switchNum = $this->args['run'];
            $list = ScanList::getInstance()->getCommandList();
            if(!isset($list[$switchNum])){
                printf("请使用选择正确的序列!\n");
                return;
            }
            list($class,$method) = $list[$switchNum];
            (new $class)->$method();
            return;
        }

        printf("请使用php Main.php --help 获取帮助!\n");
    }


    private function log()
    {
        printf("command 参数如下:\n");
        printf("\t --help :\t\t 获取帮助\n");
        printf("\t --list :\t\t 获取支持的抓取列表\n");
        printf("\t --run :\t\t --run (列表中序列运行)\n");
    }

    private function initOs()
    {
        if (PHP_OS == "WINNT") {
            exec('chcp 65001');
        }
    }
}